package orchestration.android.execution.tasks;

/**
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public class AidlMethod {

	/**
	 * @uml.property  name="name"
	 */
	private String name;
	/**
	 * @uml.property  name="outputClassName"
	 */
	private String outputClassName;
	/**
	 * @uml.property  name="paramClassNames"
	 */
	private String[] paramClassNames;
	/**
	 * @uml.property  name="params"
	 */
	private String[] params;
	/**
	 * @uml.property  name="id"
	 */
	private int id;
	
	/**
	 * @return
	 * @uml.property  name="name"
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name
	 * @uml.property  name="name"
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return
	 * @uml.property  name="outputClassName"
	 */
	public String getOutputClassName() {
		return outputClassName;
	}
	/**
	 * @param outputClassName
	 * @uml.property  name="outputClassName"
	 */
	public void setOutputClassName(String outputClassName) {
		this.outputClassName = outputClassName;
	}
	/**
	 * @return
	 * @uml.property  name="paramClassNames"
	 */
	public String[] getParamClassNames() {
		return paramClassNames;
	}
	/**
	 * @param paramClassNames
	 * @uml.property  name="paramClassNames"
	 */
	public void setParamClassNames(String[] paramClassNames) {
		this.paramClassNames = paramClassNames;
	}
	/**
	 * @return
	 * @uml.property  name="params"
	 */
	public String[] getParams() {
		return params;
	}
	/**
	 * @param params
	 * @uml.property  name="params"
	 */
	public void setParams(String[] params) {
		this.params = params;
	}
	/**
	 * @param id
	 * @uml.property  name="id"
	 */
	public void setId(int id) {
		this.id=id;
	}
	/**
	 * @return
	 * @uml.property  name="id"
	 */
	public int getId() {
		return id;
	}
	
	public static AidlMethod parse(String line) {
		AidlMethod result= new AidlMethod();
		String[] parts=line.trim().substring(0,line.lastIndexOf(")")-1).split("\\(");
		String[] first=parts[0].trim().split(" ");
		String outputClassName= first[0];
		result.setOutputClassName(outputClassName);
		String methodName=first[first.length-1];
		result.setName(methodName);

		if(parts.length==2 && !parts[1].trim().equals(")")){
			String[] second=parts[1].trim().split(",");
			String[] paramTypes= new String[second.length];
			String[] params= new String[second.length];
			for(int i=0;i<second.length;i++){
				String[] input=second[i].trim().split(" ");
				if(input.length==3){ //in or out specified
					paramTypes[i]=input[1];
					params[i]=input[2];
				}else
					paramTypes[i]=input[0];
					params[i]=input[1];
			}
			result.setParamClassNames(paramTypes);
			result.setParams(params);
		}	
		return result;
	}
}
